<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理" name="first">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="tableData" style="width: 100%">
              <!-- type:index会自动生成序号 -->
              <el-table-column label="序号" type="index" width="50">
                <template scope="scope">
                  <span>{{ (params.page - 1) * params.pagesize + scope.$index + 1 }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="name" label="角色名称" width="180" />
              <el-table-column prop="description" label="描述" />
              <el-table-column width="300" label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="hAssign(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 放置分页组件 -->
              <el-pagination
                background
                :page-size="params.pagesize"
                layout="prev, pager, next"
                :total="total"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 新增弹框 -->
      <el-dialog
        :title="cTitle"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog=false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          <!-- 右上角关闭按钮 / 蒙层点击  这俩触发的都是弹框关闭事件-->
          <!-- <el-dialog title="新增角色" :visible.sync="showDialog" @close="btnCancel" /> -->
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限的弹层 -->
      <el-dialog
        title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialogAssign"
      >
        <AssignPermission
          v-if="showDialogAssign"
          :role-id="roleId"
          @close="showDialogAssign=false"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import AssignPermission from './assignPermission'
import { getRoles, deleteRole, addRole, updateRole } from '@/api/setting'
export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      roleId: ' ',
      activeName: 'first',
      tableData: [],
      showDialogAssign: false, // 控制角色权限分配弹层是否显示
      params: {
        page: 1, // 查询第一页
        pagesize: 2 // 每页两条  --- 要与pagination中page-size一致
      },
      total: 0, // 查询结果的总条数
      showDialog: false,
      isEidt: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    cTitle() {
      return this.isEidt ? '编辑角色' : '新增角色 '
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async doAdd() {
      try {
        const rs = await addRole(this.roleForm)
        console.log('添加之后的结果', rs)

        // 1. 重新加载数据
        this.loadRoles()
        // 2. 提示一下
        this.$message.success('添加成功')
        // 3. 关闭弹层
        this.showDialog = false
      } catch (err) {
        // console.log(err.message)
        this.$message.error('添加失败')
      }
    },
    async doDel(id) {
      // 1. 去做删除
      console.log('用户点击删除', id)
      const rs = await deleteRole(id)
      console.log('删除之后的返回值', rs)
      // 2. 提示用户
      this.$message({
        type: 'success',
        message: '删除成功!'
      })
      if (this.tableData.length === 1) {
        this.params.page--
        if (this.params.page <= 0) {
          this.params.page = 1
        }
      }
      // 3. 更新页面- 重发请求
      this.loadRoles()
    },
    async doEdit() {
      try {
        const rs = await updateRole(this.roleForm)
        console.log('修改之后的结果', rs)

        // 1. 重新加载数据
        this.loadRoles()
        // 2. 提示一下
        this.$message.success('操作成功')
        // 3. 关闭弹层
        this.showDialog = false
      } catch (err) {
        // console.log(err.message)
        this.$message.error('编辑失败')
      }
    },
    async loadRoles() {
      try {
        const res = await getRoles(this.params)
        console.log('所有角色信息', res)
        // 保存数据
        this.tableData = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },
    hAdd() {
      this.showDialog = true
      this.isEidt = false
    },
    // 用户点击了删除
    hDel(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {
      })
    },
    // 用户点击了编辑
    hEdit({ id, name, description }) {
      // 1. 把当前的数据直接给表单
      // this.roleForm = row
      // this.roleForm = { id: row.id, name: row.name, description: row.description }
      this.roleForm = { id, name, description }
      // 2. 显示弹层
      this.showDialog = true
      // console.log(row)

      // 3. 设为编辑状态
      this.isEidt = true
    },
    //  用户点击了页码
    hCurrentChange(curPage) {
      // 1. 更新查询条件中页码
      this.params.page = curPage
      // alert(curPage)
      // 2. 再发一次请求
      this.loadRoles()
    },
    hSubmit() {
      console.log('提交')
      this.$refs.roleForm.validate(valid => {
        if (valid) {
          // 做具体的添加动作
          this.isEidt ? this.doEdit() : this.doAdd()
        }
      })
    },
    hClose() {
      console.log('close...............')
      // 重置表单
      this.roleForm = { // 表单数据项
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    },
    // 用户点击分配权限
    hAssign(id) {
      // 更新id
      this.roleId = id
      // 显示弹层
      this.showDialogAssign = true
    }
  }
}

</script>
